package com.car.loan.project.xr.util;

import com.car.loan.common.utils.ObjectStruct;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.redis.RedisCache;
import com.car.loan.project.cm.domain.CmBank;
import com.car.loan.project.cm.domain.CmDealer;
import com.car.loan.project.cm.mapper.CmBankMapper;
import com.car.loan.project.cm.mapper.CmDealerMapper;
import com.car.loan.project.system.domain.SysDept;
import com.car.loan.project.system.domain.SysUser;
import com.car.loan.project.system.mapper.SysDeptMapper;
import com.car.loan.project.system.mapper.SysUserMapper;
import com.car.loan.project.xr.domain.Loan;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * @author huangding
 * @description 获取redis 缓存数据
 * @date 2019/11/15 14:43
 */
@Component
public class RedisDataUtil {

    public static final String REDIS_BANK_MAP_KEY = "bank_map";

    public static final String REDIS_DEPT_MAP_KEY = "dept_map";


    public static final String REDIS_USER_MAP_KEY = "user_map";

    public static final String REDIS_DEALER_MAP_KEY = "dealer_map";


    private static final int EFFECTIVE_TIME = 7200;

    @Resource
    private RedisCache redisCache;

    @Resource
    private CmBankMapper cmBankMapper;

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private CmDealerMapper cmDealerMapper;

    /**
     * 获取银行缓存数据
     */
    public Map<String, CmBank> getRedisBankMap() {
        return redisCache
            .getCacheMapToSupplier(REDIS_BANK_MAP_KEY, () -> cmBankMapper.selectList(null).stream()
                .collect(Collectors.toMap(b -> String.valueOf(b.getId()), b -> b)), EFFECTIVE_TIME);
    }

    public void reloadBankRedis() {
        redisCache.deleteObject(REDIS_BANK_MAP_KEY);
        getRedisBankMap();
    }


    public void reloadDeptRedis() {
        redisCache.deleteObject(REDIS_DEPT_MAP_KEY);
        getRedisDeptMap();
    }

    public void reloadUserRedis() {
        redisCache.deleteObject(REDIS_USER_MAP_KEY);
        getRedisUserMap();
    }

    public void reloadDealerRedis() {
        redisCache.deleteObject(REDIS_DEALER_MAP_KEY);
        getRedisDealerMap();
    }

    /**
     * 从新加载
     */
    public void reloadRedis() {
        reloadBankRedis();
        reloadDeptRedis();
        reloadUserRedis();
        reloadDealerRedis();
    }

    /**
     * 获取部门缓存数据
     */
    public Map<String, SysDept> getRedisDeptMap() {
        return redisCache.getCacheMapToSupplier(REDIS_DEPT_MAP_KEY,
            () -> sysDeptMapper.selectDeptList(null).stream()
                .collect(Collectors.toMap(b -> String.valueOf(b.getDeptId()), b -> b)),
            EFFECTIVE_TIME);
    }

    /**
     * 获取系统用户缓存
     */
    public Map<String, SysUser> getRedisUserMap() {
        return redisCache.getCacheMapToSupplier(REDIS_USER_MAP_KEY,
            () -> sysUserMapper.selectUserList(null).stream()
                .collect(Collectors.toMap(b -> String.valueOf(b.getUserId()), b -> b)),
            EFFECTIVE_TIME);
    }

    /**
     * 获取经销商缓存
     */
    public Map<String, CmDealer> getRedisDealerMap() {
        return redisCache.getCacheMapToSupplier(REDIS_DEALER_MAP_KEY,
            () -> cmDealerMapper.selectList(null).stream()
                .collect(Collectors.toMap(b -> String.valueOf(b.getId()), b -> b)),
            EFFECTIVE_TIME);
    }

    /**
     * 获取经销商名称
     */
    public String getDealerName(Long id) {
        if (null == id) {
            return StringUtils.EMPTY;
        }
        CmDealer cmDealer = getRedisDealerMap().get(String.valueOf(id));
        if (null == cmDealer) {
            return StringUtils.EMPTY;
        }
        return cmDealer.getName();
    }

    /**
     * 获取业务员名称
     */
    public String getAdminName(Long id) {
        if (null == id) {
            return StringUtils.EMPTY;
        }
        SysUser sysUser = getRedisUserMap().get(String.valueOf(id));
        if (null == sysUser) {
            return StringUtils.EMPTY;
        }
        return sysUser.getUserName();
    }

    public SysUser getAdmin(Long id) {
        if (null == id) {
            return null;
        }
        return getRedisUserMap().get(String.valueOf(id));
    }

    /**
     * 获取获取部门名称
     */
    public String getDeptName(Long id) {
        if (null == id) {
            return StringUtils.EMPTY;
        }
        SysDept sysDept = getRedisDeptMap().get(String.valueOf(id));
        if (null == sysDept) {
            return StringUtils.EMPTY;
        }
        return sysDept.getDeptName();
    }

    /**
     * 获取获取银行名称
     */
    public String getBankName(Long id) {
        if (null == id) {
            return StringUtils.EMPTY;
        }
        CmBank cmBank = getRedisBankMap().get(String.valueOf(id));
        if (null == cmBank) {
            return StringUtils.EMPTY;
        }
        return cmBank.getAlias();
    }


    public <T> String getMapClassToDefaultMethod(Map<String, T> map, Long key) {
        return getMapClassToMethod(map, String.valueOf(key), "getName");

    }

    public <T> String getMapClassToMethod(Map<String, T> map, Long key, String method) {
        return getMapClassToMethod(map, String.valueOf(key), method);
    }

    public <T> String getMapClassToMethod(Map<String, T> map, String key, String method) {
        if (null == key) {
            return StringUtils.EMPTY;
        }
        T t = map.get(key);
        if (null == t) {
            return StringUtils.EMPTY;
        }
        ObjectStruct objectStruct = new ObjectStruct(t);
        return (String) objectStruct.getMethod(method, null);
    }


}
